//
//  KDRepeatsController.m
//  KongDays
//
//  Created by deeplu on 13. 1. 5..
//  Copyright (c) 2013년 BlueBeans. All rights reserved.
//

#import "KDRepeatController.h"
#import "KDRepeat.h"

@interface KDRepeatController ()

@end

@implementation KDRepeatController

@synthesize defaultRepeat = defaultRepeat_;
@synthesize delegate = delegate_;
@synthesize repeatsTable;

- (void)dealloc
{
    [repeatsList release];
    
    [self.defaultRepeat release];
    [self.repeatsTable release];
    [super dealloc];
}

- (id)init
{
    self = [super initWithNibName:@"KDRepeatController" bundle:nil];
    if (self) {
        
    }
    
    return self;
}

#pragma mark - UIViewController methods

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = NSLocalizedString(@"Repeat", nil);
    
    repeatsList = [[KDRepeat all] retain];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - UITableViewDelegate methods

-       (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.defaultRepeat = (KDRepeat *)[repeatsList objectAtIndex:indexPath.row];
    
    [self.repeatsTable reloadData];
    
    [self.delegate repeatDidChange:self.defaultRepeat];
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UITableViewDataSource methods

- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section;
{
    if (repeatsList != nil)
        return [repeatsList count];
    else
        return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell =
    [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        cell = [[[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                 reuseIdentifier:CellIdentifier] autorelease];
    }
    
    if (repeatsList) {
        
        KDRepeat *repeat = (KDRepeat *)[repeatsList objectAtIndex:indexPath.row];
        cell.textLabel.text = repeat.displayText;
        
        if ([repeat isEqual:self.defaultRepeat]) {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
    }
    
    return cell;
}

@end
